<?php

class tour_model extends FlexiGrid_Model {

    function __construct()
    {
        parent::__construct();
        
    }
    function get($ID){
        $where=array("ID"=>$ID);
        $query=$this->db->get_where('tbl_tour', $where); 
        return $query->result();
    }
    function gets(){
        $query=$this->db
                ->select("ID,Title")
                ->get_where('tbl_tour'); 
        return $query->result();
    }
    function insert($params){
        $this->db->set('Insert', 'NOW()', FALSE);
        $this->db->insert('tbl_tour', $params); 
        $count = $this->db->affected_rows(); //should return the number of rows affected by the last query
        if($count==1) return true;
        return false;
    }
    function delete($ID){
        $this->db->set('Delete', 'NOW()', FALSE);
        $where=array("ID"=>$ID);
        $this->db->where($where);
        $this->db->update('tbl_tour'); 
        $count = $this->db->affected_rows(); //should return the number of rows affected by the last query
        if($count==1) return true;
        return false;
    }
    function retore($ID){
        $this->db->set('Delete', 'NULL', FALSE);
        $where=array("ID"=>$ID);
        $this->db->where($where);
        $this->db->update('tbl_tour'); 
        $count = $this->db->affected_rows(); //should return the number of rows affected by the last query
        if($count==1) return true;
        return false;
    }
    function update($ID,$params){
        $this->db->set('Update', 'NOW()', FALSE);
        $this->db->where('ID', $ID);
        $this->db->update('tbl_tour', $params); 
        $count = $this->db->affected_rows(); //should return the number of rows affected by the last query
        if($count==1) return true;
        return false;
    }
    function FlexiGridData(){
        $configs["strQuery"]="
            SELECT SQL_CALC_FOUND_ROWS `tbl_tour`.*
            FROM `tbl_tour`
            ";
        $configs["strWhere"]="
            WHERE true
            ";
        if(isset($_SESSION["ADP"]["Filter_".CCTRL]["Filters"])){
            if($_SESSION["ADP"]["Filter_".CCTRL]["FilterType"]=="none"){
                $configs["strWhere"].=" AND false ";
            }elseif($_SESSION["ADP"]["Filter_".CCTRL]["FilterType"]=="me"){
                $strFilters="";
                foreach ($_SESSION["ADP"]["Filter_".CCTRL]["Filters"] as $f){
                    $f=  mysql_real_escape_string($f);
                    $strFilters.="'$f',";
                }
                $strFilters.="'0-9a-zA-Z'";
                $configs["strWhere"].=" AND `tbl_tour`.Type in ($strFilters) ";
            }
        }
        if( $_SESSION["ADP"][CCTRL]["display"]==0 ){
            $configs["strWhere"].=" AND `tbl_tour`.`Delete` IS NULL";
        }elseif( $_SESSION["ADP"][CCTRL]["display"]==-1 ){
            $configs["strWhere"].=" AND `tbl_tour`.`Delete` IS NOT NULL";
        }
        $configs["strOrderBy"]=" ORDER BY `Insert` DESC";
        $configs["fields"]=array(
            
            );
        $configs["filterfields"]=array(
            "Title","Alias","Type"
        );
        $this->init($configs);
        return $this->Binding();
    }
    function FlexiGridData_History($tour=""){
        $configs["strQuery"]="
            SELECT SQL_CALC_FOUND_ROWS `tbl_reg`.*,`tbl_tour`.`Title`
            FROM `tbl_tour`,`tbl_reg`
            ";
        $configs["strWhere"]="
            WHERE `tbl_reg`.Tour=`tbl_tour`.`ID`
            ";
        if($tour!=""){
            $tour=  (int)$tour;
            $configs["strWhere"]="
            AND `tbl_tour`.ID='$tour'
            ";
        }
        if(isset($_SESSION["ADP"]["Filter_".CCTRL]["Filters"])){
            if($_SESSION["ADP"]["Filter_".CCTRL]["FilterType"]=="none"){
                $configs["strWhere"].=" AND false ";
            }elseif($_SESSION["ADP"]["Filter_".CCTRL]["FilterType"]=="me"){
                $strFilters="";
                foreach ($_SESSION["ADP"]["Filter_".CCTRL]["Filters"] as $f){
                    $f=  mysql_real_escape_string($f);
                    $strFilters.="'$f',";
                }
                $strFilters.="'0-9a-zA-Z'";
                $configs["strWhere"].=" AND `tbl_tour`.ID in ($strFilters) ";
            }
        }
        $configs["strOrderBy"]=" ORDER BY `tbl_reg`.`Insert` DESC";
        $configs["fields"]=array(
            "ID"    =>"`tbl_reg`.`ID`",
            "Status"=>"`tbl_reg`.`Status`",
            "Insert"=>"`tbl_reg`.`Insert`",
            "Delete"=>"`tbl_reg`.`Delete`",
            "Update"=>"`tbl_reg`.`Update`",
            "Name"  =>"`tbl_reg`.`Name`",
            "Tour" =>"`tbl_tour`.`Title`",
            "FullName" =>"`tbl_reg`.`FullName`"
            );
        $configs["filterfields"]=array(
            "FullName","Tour"
        );
        $this->init($configs);
        return $this->Binding();
    }
}
?>
